#!/usr/bin/env python3
# by zhtsu

# GPLv3
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import pygame
import pygame_menu
from pygame.locals import *
from sys import exit
import time
import random
from res import *
from GameSprite import *

#数据统计
used_cards_num = 0
kill_enemy_num = 0

# 游戏标题
GAME_TITLE = u"最后防线"
# 帧率
FPS = 60
# 屏幕尺寸
SCREEN_SIZE = (960, 640)
# 初始化窗口
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
pygame.display.set_caption(GAME_TITLE)

clock = pygame.time.Clock()
# 精灵组
enemy_group = pygame.sprite.Group()
brave_group = pygame.sprite.Group()
huntress_group = pygame.sprite.Group()
rank_group = pygame.sprite.Group()
swordman_group = pygame.sprite.Group()
ronin_group = pygame.sprite.Group()
card_group = pygame.sprite.Group()
corpse_group = pygame.sprite.Group()
home_group = pygame.sprite.Group()
# 创建死亡使者精灵
bringer_of_deaths = [
    GameSprite("bringer-of-death",
               IMAGES["BringerOfDeath_walk"], 140, 93, 8, random.randint(900, 1000), 0, 1),
    GameSprite("bringer-of-death",
               IMAGES["BringerOfDeath_walk"], 140, 93, 8, random.randint(900, 1000), 80, 1),
    GameSprite("bringer-of-death",
               IMAGES["BringerOfDeath_walk"], 140, 93, 8, random.randint(900, 1000), 160, 1),
    GameSprite("bringer-of-death",
               IMAGES["BringerOfDeath_walk"], 140, 93, 8, random.randint(900, 1000), 240, 1)
]

#创建敌人精灵
enemys = [
    
]

# 创建基地精灵
homes = [
    GameSprite("home", IMAGES["Home"], 96, 96, 1, 6, 30, 0),
    GameSprite("home", IMAGES["Home"], 96, 96, 1, 6, 156, 0),
    GameSprite("home", IMAGES["Home"], 96, 96, 1, 6, 282, 0),
]
for home in homes:
    home_group.add(home)

#基地血量
home_HP = 10000

# 创建卡片精灵
card_origin_pos = [(92, 472), (254, 472), (414, 472), (572, 472), (734, 472)]
card_cool_time = [0.0, 0.0, 0.0, 0.0, 0.0]
card_move = [False, False, False, False, False]
card_is_used = [False, False, False, False, False]
cards = [
    GameSprite("card", IMAGES["Card_rank"], 130, 171, 1, 92, 472, 0),
    GameSprite("card", IMAGES["Card_huntress"], 130, 171, 1, 254, 472, 0),
    GameSprite("card", IMAGES["Card_swordman"], 130, 171, 1, 414, 472, 0),
    GameSprite("card", IMAGES["Card_brave"], 130, 171, 1, 572, 472, 0),
    GameSprite("card", IMAGES["Card_ronin"], 130, 171, 1, 734, 472, 0)
]
for enemy in bringer_of_deaths:
    enemy_group.add(enemy)
cooling_time = 10
for card in cards:
    card.set_cooling_time(cooling_time)
    card_group.add(card)
    cooling_time *= 2

# 字体
HP_font = pygame.font.Font(FONTS["Silver"], 50)

# 鼠标光标图像
mouse_cursor = pygame.image.load(IMAGES["Cursor"]).convert_alpha()

#生成新敌人
def create_enemy(num):
    for i in range(num):
        enemy.add(GameSprite("bringer-of-death",
               IMAGES["BringerOfDeath_walk"], 140, 93, 8, random.randint(900, 1000), random.randint(32, 250), 1))

def MainGame():
    global home_HP, used_cards_num, kill_enemy_num
    home_HP = 10000
    game_level = 1
    is_loop = True
    while is_loop:
        clock.tick(60)
        ticks = pygame.time.get_ticks()

        bg = pygame.image.load(IMAGES["GameMap_green"])
        screen.blit(bg, [0, 0])

        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                if cards[0].getPos().collidepoint(x, y) and not card_is_used[0]:
                    card_move[0] = True
                elif cards[1].getPos().collidepoint(x, y) and not card_is_used[1]:
                    card_move[1] = True
                elif cards[2].getPos().collidepoint(x, y) and not card_is_used[2]:
                    card_move[2] = True
                elif cards[3].getPos().collidepoint(x, y) and not card_is_used[3]:
                    card_move[3] = True
                elif cards[4].getPos().collidepoint(x, y) and not card_is_used[4]:
                    card_move[4] = True
            if event.type == pygame.MOUSEBUTTONUP:
                #使用卡牌放置英雄
                #判断放置位置是否合法
                x, y = pygame.mouse.get_pos()
                if x > 128 and x < 928 and y > 32 and y < 376:
                    if x >= 832 and x <= 896 and y >= 32 and y <= 64:
                        is_can_put = False
                    else :
                        is_can_put = True
                else :
                    is_can_put = False
                #士兵
                if card_move[0] == True and is_can_put:
                    #判断卡牌是否被移动
                    card_move[0] = False
                    #判断卡牌是否被使用
                    card_is_used[0] = True
                    #获取开始冷却时间
                    card_cool_time[0] = time.time()
                    origin_x, origin_y = card_origin_pos[0]
                    cards[0].moveByTopLeft(origin_x, origin_y)
                    cards[0].change_image(IMAGES["Card_rank_time"])
                    rank = GameSprite("rank", IMAGES["Rank_idle"], 90, 67, 4, x, y, 0)
                    rank.moveByCenter(x, y)
                    rank_group.add(rank)
                    used_cards_num += 1
                elif card_move[0] == True and not is_can_put:
                    card_move[0] = False
                    origin_x, origin_y = card_origin_pos[0]
                    cards[0].moveByTopLeft(origin_x, origin_y)
                #射手
                if card_move[1] == True and is_can_put:
                    card_move[1] = False
                    card_is_used[1] = True
                    card_cool_time[1] = time.time()
                    origin_x, origin_y = card_origin_pos[1]
                    cards[1].moveByTopLeft(origin_x, origin_y)
                    cards[1].change_image(IMAGES["Card_huntress_time"])
                    huntress = GameSprite("hunress", IMAGES["Huntress_idle"], 100, 100, 6, x, y, 0)
                    huntress.moveByCenter(x, y)
                    huntress_group.add(huntress)
                    used_cards_num += 1
                elif card_move[1] == True and not is_can_put:
                    card_move[1] = False
                    origin_x, origin_y = card_origin_pos[1]
                    cards[1].moveByTopLeft(origin_x, origin_y)
                #剑士
                if card_move[2] == True and is_can_put:
                    card_move[2] = False
                    card_is_used[2] = True
                    card_cool_time[2] = time.time()
                    origin_x, origin_y = card_origin_pos[2]
                    cards[2].moveByTopLeft(origin_x, origin_y)
                    cards[2].change_image(IMAGES["Card_swordman_time"])
                    swordman = GameSprite("swordman", IMAGES["Swordman_idle"], 140, 140, 7, x, y, 0)
                    swordman.moveByCenter(x, y)
                    swordman_group.add(swordman)
                    used_cards_num += 1
                elif card_move[2] == True and not is_can_put:
                    card_move[2] = False
                    origin_x, origin_y = card_origin_pos[2]
                    cards[2].moveByTopLeft(origin_x, origin_y)
                #勇者
                if card_move[3] == True and is_can_put:
                    card_move[3] = False
                    card_is_used[3] = True
                    origin_x, origin_y = card_origin_pos[3]
                    card_cool_time[3] = time.time()
                    cards[3].moveByTopLeft(origin_x, origin_y)
                    cards[3].change_image(IMAGES["Card_brave_time"])
                    brave = GameSprite("brave", IMAGES["Brave_idle"], 64, 44, 6, x, y, 0)
                    brave.moveByCenter(x, y)
                    brave_group.add(brave)
                    used_cards_num += 1
                elif card_move[3] == True and not is_can_put:
                    card_move[3] = False
                    origin_x, origin_y = card_origin_pos[3]
                    cards[3].moveByTopLeft(origin_x, origin_y)
                #浪人
                if card_move[4] == True and is_can_put:
                    card_move[4] = False
                    card_is_used[4] = True
                    card_cool_time[4] = time.time()
                    origin_x, origin_y = card_origin_pos[4]
                    cards[4].moveByTopLeft(origin_x, origin_y)
                    cards[4].change_image(IMAGES["Card_ronin_time"])
                    ronin = GameSprite("ronin", IMAGES["Ronin_idle"], 150, 150, 6, x, y, 0)
                    ronin.moveByCenter(x, y)
                    ronin_group.add(ronin)
                    used_cards_num += 1
                elif card_move[4] == True and not is_can_put:
                    card_move[4] = False
                    origin_x, origin_y = card_origin_pos[4]
                    cards[4].moveByTopLeft(origin_x, origin_y)

        #更新精灵组
        home_group.update(ticks)
        home_group.draw(screen)
        enemy_group.update(ticks)
        enemy_group.draw(screen)
        brave_group.update(ticks)
        brave_group.draw(screen)
        huntress_group.update(ticks)
        huntress_group.draw(screen)
        rank_group.update(ticks)
        rank_group.draw(screen)
        swordman_group.update(ticks)
        swordman_group.draw(screen)
        ronin_group.update(ticks)
        ronin_group.draw(screen)

        #控制游戏难度
        # if kill_enemy_num % 20 == 0:
        #     if game_level != 1:
        #         game_level += 1
        #     level_show = HP_font.render("Round {}".format(game_level), True, (255, 0, 0))
        #     screen.blit(level_show, (480, 320))
        #     pygame.display.update()
        #     is_pause = True
        #     while is_pause:
        #         for event in pygame.event.get():
        #             if event == pygame.MOUSEBUTTONDOWN:
        #                 is_pause == False
        #士兵和敌人的碰撞
        collide_with_enemy = pygame.sprite.groupcollide(
            rank_group, enemy_group, False, False)
        if collide_with_enemy:
            for rank in collide_with_enemy.keys():
                enemys = collide_with_enemy[rank]
                for enemy in enemys:
                    if pygame.sprite.collide_mask(rank, enemy):
                        rank.changeAction(IMAGES["Rank_attack"])
                        rank.deductHP(10)
                        enemy.changeAction(IMAGES["BringerOfDeath_attack"])
                        enemy.changeSpeed(0)
                        enemy.deductHP(10)
                        if rank.HP < 0:
                            rank_group.remove(rank)
                            for e in enemys:
                                e.changeAction(IMAGES["BringerOfDeath_walk"])
                                e.changeSpeed(1)
                            break
                        elif enemy.HP < 0:
                            enemy_group.remove(enemy)
                            enemy_group.add(GameSprite("bringer-of-death",
                            IMAGES["BringerOfDeath_walk"], 140, 93, 8, random.randint(900, 1000), random.randint(32, 250), 1))
                            rank.changeAction(IMAGES["Rank_idle"])
                            kill_enemy_num += 1
        #射手和敌人的碰撞
        collide_with_enemy = pygame.sprite.groupcollide(
            huntress_group, enemy_group, False, False)
        if collide_with_enemy:
            for huntress in collide_with_enemy.keys():
                enemys = collide_with_enemy[huntress]
                for enemy in enemys:
                    if pygame.sprite.collide_mask(huntress, enemy):
                        huntress.changeAction(IMAGES["Huntress_attack"])
                        huntress.deductHP(15)
                        enemy.changeAction(IMAGES["BringerOfDeath_attack"])
                        enemy.changeSpeed(0)
                        enemy.deductHP(10)
                        if huntress.HP < 0:
                            huntress_group.remove(huntress)
                            for e in enemys:
                                e.changeAction(IMAGES["BringerOfDeath_walk"])
                                e.changeSpeed(1)
                            break
                        elif enemy.HP < 0:
                            enemy_group.remove(enemy)
                            enemy_group.add(GameSprite("bringer-of-death",
                            IMAGES["BringerOfDeath_walk"], 140, 93, 8, random.randint(900, 1000), random.randint(32, 250), 1))
                            huntress.changeAction(IMAGES["Huntress_idle"])
                            kill_enemy_num += 1
        #剑士和敌人的碰撞
        collide_with_enemy = pygame.sprite.groupcollide(
            swordman_group, enemy_group, False, False)
        if collide_with_enemy:
            for swordman in collide_with_enemy.keys():
                enemys = collide_with_enemy[swordman]
                for enemy in enemys:
                    if pygame.sprite.collide_mask(swordman, enemy):
                        swordman.changeAction(IMAGES["Swordman_attack"])
                        swordman.deductHP(10)
                        enemy.changeAction(IMAGES["BringerOfDeath_attack"])
                        enemy.changeSpeed(0)
                        enemy.deductHP(20)
                        if swordman.HP < 0:
                            swordman_group.remove(swordman)
                            for e in enemys:
                                e.changeAction(IMAGES["BringerOfDeath_walk"])
                                e.changeSpeed(1)
                            break
                        elif enemy.HP < 0:
                            enemy_group.remove(enemy)
                            enemy_group.add(GameSprite("bringer-of-death",
                            IMAGES["BringerOfDeath_walk"], 140, 93, 8, random.randint(900, 1000), random.randint(32, 250), 1))
                            swordman.changeAction(IMAGES["Swordman_idle"])
                            kill_enemy_num += 1
        #勇者和敌人的碰撞
        collide_with_enemy = pygame.sprite.groupcollide(
            brave_group, enemy_group, False, False)
        if collide_with_enemy:
            for brave in collide_with_enemy.keys():
                enemys = collide_with_enemy[brave]
                for enemy in enemys:
                    if pygame.sprite.collide_mask(brave, enemy):
                        brave.changeAction(IMAGES["Brave_attack"])
                        brave.deductHP(2)
                        enemy.changeAction(IMAGES["BringerOfDeath_attack"])
                        enemy.changeSpeed(0)
                        enemy.deductHP(10)
                        if brave.HP < 0:
                            brave_group.remove(brave)
                            for e in enemys:
                                e.changeAction(IMAGES["BringerOfDeath_walk"])
                                e.changeSpeed(1)
                            break
                        elif enemy.HP < 0:
                            enemy_group.remove(enemy)
                            enemy_group.add(GameSprite("bringer-of-death",
                            IMAGES["BringerOfDeath_walk"], 140, 93, 8, random.randint(900, 1000), random.randint(32, 250), 1))
                            brave.changeAction(IMAGES["Brave_idle"])
                            kill_enemy_num += 1
        #浪人和敌人的碰撞
        collide_with_enemy = pygame.sprite.groupcollide(
            ronin_group, enemy_group, False, False)
        if collide_with_enemy:
            for ronin in collide_with_enemy.keys():
                enemys = collide_with_enemy[ronin]
                for enemy in enemys:
                    if pygame.sprite.collide_mask(ronin, enemy):
                        ronin.changeAction(IMAGES["Ronin_attack"])
                        ronin.deductHP(10)
                        enemy.changeAction(IMAGES["BringerOfDeath_attack"])
                        enemy.changeSpeed(0)
                        enemy.deductHP(50)
                        if ronin.HP < 0:
                            ronin_group.remove(ronin)
                            for e in enemys:
                                e.changeAction(IMAGES["BringerOfDeath_walk"])
                                e.changeSpeed(1)
                            break
                        elif enemy.HP < 0:
                            enemy_group.remove(enemy)
                            enemy_group.add(GameSprite("bringer-of-death",
                            IMAGES["BringerOfDeath_walk"], 140, 93, 8, random.randint(900, 1000), random.randint(32, 250), 1))
                            ronin.changeAction(IMAGES["Ronin_idle"])
                            kill_enemy_num += 1
        # 敌人和基地的碰撞
        collide_with_home = pygame.sprite.groupcollide(
            enemy_group, home_group, False, False)
        if collide_with_home:
            for enemy in collide_with_home.keys():
                homes = collide_with_home[enemy]
                for home in homes:
                    if pygame.sprite.collide_circle_ratio(0.5)(enemy, home):
                        enemy.changeSpeed(0)
                        enemy.changeAction(IMAGES["BringerOfDeath_attack"])
                        home_HP -= 10

        if (home_HP < 0):
            print("你失去了最后一道防线！")
            is_loop = False



        # 显示血量
        home_HP_show = HP_font.render(
            u"防线剩余生命值： {}0% 已使用卡牌数： {} 已击杀敌人数： {}".format((home_HP // 1000), used_cards_num, kill_enemy_num), True, (255, 243, 208))
        screen.blit(home_HP_show, (64, 430))

        #绘制卡牌
        card_group.update(ticks)
        card_group.draw(screen)

        #卡牌移动
        if card_move[0] == True:
            now_x, now_y = pygame.mouse.get_pos()
            cards[0].moveByTopLeft(now_x, now_y)
        elif card_move[1] == True:
            now_x, now_y = pygame.mouse.get_pos()
            cards[1].moveByTopLeft(now_x, now_y)
        elif card_move[2] == True:
            now_x, now_y = pygame.mouse.get_pos()
            cards[2].moveByTopLeft(now_x, now_y)
        elif card_move[3] == True:
            now_x, now_y = pygame.mouse.get_pos()
            cards[3].moveByTopLeft(now_x, now_y)
        elif card_move[4] == True:
            now_x, now_y = pygame.mouse.get_pos()
            cards[4].moveByTopLeft(now_x, now_y)

        #计算卡牌冷却时间
        if time.time() - card_cool_time[0] > cards[0].get_cooling_time():
            card_is_used[0] = False
            cards[0].change_image(IMAGES["Card_rank"])
        if time.time() - card_cool_time[1] > cards[1].get_cooling_time():
            card_is_used[1] = False
            cards[1].change_image(IMAGES["Card_huntress"])
        if time.time() - card_cool_time[2] > cards[2].get_cooling_time():
            card_is_used[2] = False
            cards[2].change_image(IMAGES["Card_swordman"])
        if time.time() - card_cool_time[3] > cards[3].get_cooling_time():
            card_is_used[3] = False
            cards[3].change_image(IMAGES["Card_brave"])
        if time.time() - card_cool_time[4] > cards[4].get_cooling_time():
            card_is_used[4] = False
            cards[4].change_image(IMAGES["Card_ronin"])

        # 绘制鼠标光标
        x, y = pygame.mouse.get_pos()
        screen.blit(mouse_cursor, (x, y))

        pygame.display.update()

#游戏菜单
menu = pygame_menu.Menu(300,
                        400,
                        "Game",
                        theme=pygame_menu.themes.THEME_GREEN)

menu.add_text_input("LastDefenseLine")
menu.add_button('Play', MainGame)
menu.add_button('Quit', pygame_menu.events.EXIT)
menu.mainloop(screen)